package com.yituo.mjworld.mvc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.entity.web.ProductProps;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.BaseService;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductPropsServiceImpl extends BaseService {
  /**
   * 获取某件商品的属性
   *
   * @param id 产品id
   * @return
   */
  public ProductProps getTemplate(int id) {
    String hql = "from ProductProps where productId=:productId";
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("productId", id);
    List<ProductProps> list = baseDao.find(hql, params);
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }

  /**
   * 获取某件商品的属性
   *
   * @param form 查询语句
   * @return
   */
  @Override
  public JSONObject getList(QueryForm form) {
    return null;
  }

  /**
   * 获取某件商品的属性
   *
   * @param id 查询语句
   * @return
   */
  public List<ProductProps> getList(int id) {
    String hql = "from ProductProps where productId=:productId";
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("productId", id);
    List<ProductProps> list = baseDao.find(hql, params);
    return list;
  }


  /**
   * 删除一条属性
   *
   * @param form 删除表单
   * @return
   */
  @Override
  public Result delete(DeleteForm form) {
    return null;
  }

  /**
   * 删除一条属性
   *
   * @param id
   * @return
   */
  public boolean delete(int id) {
    this.baseDao.delete(ProductProps.class, id);
    return true;
  }


  /**
   * 新增一条记录
   *
   * @param baseForm 更新表单
   * @return
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   */
  @Override
  public Result saveOrUpdate(BaseForm baseForm) throws InvocationTargetException, IllegalAccessException {
    ProductProps productProps = (ProductProps) baseForm;
    this.baseDao.saveOrUpdate(productProps);
    return new Result().setResult(true);
  }
}
